package com.tzy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.tzy.entity.OrderMaster;
import com.tzy.entity.User;
import com.tzy.service.OrderMasterService;
import com.tzy.service.UserService;

/**
* <p>Title: UserController1</p> 
* <p>Description: 个人信息管理</p> 
* @author  唐振耀 
* @date  2019年5月29日
*/
@Controller
@SessionAttributes("password")
public class UserController {
	@Autowired
	private UserService us;
	@Autowired
	private OrderMasterService om;
	/**
	 * 根据user_id查询user表信息
	 * @param u
	 * @param model
	 * @return
	 */
	@RequestMapping("PersonalSettings")
	public String selectUserByName(Integer user_id,Model model,HttpServletRequest request) {
//		System.out.println("username的值是:"+u);
		User user = this.us.selectUserByName(user_id);
		System.out.println("user:"+user);
		if (user != null) {
//			Integer uid = user_id;
			System.out.println("订单表id："+user_id);
			List<OrderMaster> om = this.om.selectOrderDetailAndOrderMasterByOrder_id(user_id);
			System.out.println("订单信息："+om);
			request.getSession().setAttribute("om", om);
		}
		String password = user.getPassword();
//		System.out.println("qqqqq"+password);
		model.addAttribute("password",password);
		model.addAttribute("u", user);
		return "PersonalSettings";
	}
	/**
	 * 根据user_id修改user表信息
	 * @param u
	 * @return
	 */
	@RequestMapping("updateUserById")
	public String updateUserById(User u,Model model) {
//		System.out.println("要修改的参数是"+u);
		boolean user = this.us.updateUserById(u);
		if (true) {
			System.out.println("执行修改成功："+user);
//			model.addAttribute("Description", "修改成功");
			return "forward:/PersonalSettings";
		} else {
			model.addAttribute("Description", "修改成功");
			return "PersonalSettings";
		}
		
	}
	/**
	 * @param user_id
	 * @param pasw
	 * @param pw
	 * @param passwords
	 * @param model
	 * @param request
	 * @return 根据用户id修改密码
	 */
	@RequestMapping("updateUserByIdOnPassword")
	public String updateUserByIdOnPassword(@Param("user_id")Integer user_id,String pasw,String pw,@Param("password")String passwords,Model model,HttpServletRequest request) {
		
		Object password = request.getSession().getAttribute("password");
		if(pasw != null) {
			if (password.equals(pasw)) {
				System.out.println("原密码通过");
				if(pw != null) {
					if(pw.equals(passwords)) {
//						System.out.println("新密码验证通过");
						boolean user = this.us.updateUserByIdOnPassword(user_id,passwords);
						return "forward:/PersonalSettings";
					}else {
						model.addAttribute("prompt","重复密码与新密码不相同！！！");
						return "forward:/PersonalSettings";
					}
				}else {
					model.addAttribute("prompt","新密码不能为空！！！");
					return "forward:/PersonalSettings";
				}	
			}else {
//				System.out.println("_______"+password+"+++++++++"+pasw);
				model.addAttribute("prompt","原密码不正确！！！");
				return "forward:/PersonalSettings";
			}
		}else {
			model.addAttribute("prompt","原密码不能为空！！！");
			return "forward:/PersonalSettings";
		}	
	}
	/**
	 * @return 返回主页
	 */
	@RequestMapping("ReturnToHomePage")
	public String ReturnToHomePage() {
		return "main";
		
	}

}
